/*
 * StyleEditor_.java
 *
 * Created on 17. Oktober 2008, 16:47
 */
package de.blitzcoder.collide.config.textarea;

import de.blitzcoder.collide.Interface;
import de.blitzcoder.collide.gui.IDEDialog;
import de.blitzcoder.collide.gui.textarea.CollIDETextArea;
import de.blitzcoder.collide.util.Log;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JColorChooser;
import org.gjt.sp.jedit.syntax.SyntaxStyle;

/**
 *
 * @author  blitzcoder
 */
public class StyleEditor extends javax.swing.JDialog {

    private TextAreaTab tab;
    private PreviewPanel tabPanel;
    
    /** Creates new form StyleEditor_ */
    public StyleEditor(PreviewPanel panel,TextAreaTab tab, IDEDialog parent) {
        super(parent, true);

        initComponents();

        setTitle("Style");

        this.tab = tab;
        
        SyntaxStyle style = panel.getStyle();
        tabPanel = panel;
        
        previewPanel = new PreviewPanel(style);
        previewPanel_.setLayout(new GridLayout());
        previewPanel_.add(previewPanel);
        
        boldCheckBox.setSelected(style.getFont().isBold());
        italicCheckBox.setSelected(style.getFont().isItalic());

        backgroundPanel.setBackground(style.getBackgroundColor());
        colorPanel.setBackground(style.getForegroundColor());

        update();

        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        boldCheckBox = new javax.swing.JCheckBox();
        italicCheckBox = new javax.swing.JCheckBox();
        panel1 = new javax.swing.JPanel();
        backgroundPanel = new javax.swing.JPanel();
        panel2 = new javax.swing.JPanel();
        colorPanel = new javax.swing.JPanel();
        previewPanel_ = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Einstellungen"));
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.GridBagLayout());

        boldCheckBox.setText("Fett");
        boldCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                boldCheckBoxStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(boldCheckBox, gridBagConstraints);

        italicCheckBox.setText("Kursiv");
        italicCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                italicCheckBoxStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(italicCheckBox, gridBagConstraints);

        panel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Hintergrund"));
        panel1.setOpaque(false);
        panel1.setLayout(new java.awt.GridBagLayout());

        backgroundPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        backgroundPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                backgroundPanelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout backgroundPanelLayout = new javax.swing.GroupLayout(backgroundPanel);
        backgroundPanel.setLayout(backgroundPanelLayout);
        backgroundPanelLayout.setHorizontalGroup(
            backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 66, Short.MAX_VALUE)
        );
        backgroundPanelLayout.setVerticalGroup(
            backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 34, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        panel1.add(backgroundPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(panel1, gridBagConstraints);

        panel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Farbe"));
        panel2.setOpaque(false);
        panel2.setLayout(new java.awt.GridLayout(1, 0));

        colorPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        colorPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                colorPanelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout colorPanelLayout = new javax.swing.GroupLayout(colorPanel);
        colorPanel.setLayout(colorPanelLayout);
        colorPanelLayout.setHorizontalGroup(
            colorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 84, Short.MAX_VALUE)
        );
        colorPanelLayout.setVerticalGroup(
            colorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 34, Short.MAX_VALUE)
        );

        panel2.add(colorPanel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel1.add(panel2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.7;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jPanel1, gridBagConstraints);

        previewPanel_.setBorder(javax.swing.BorderFactory.createTitledBorder("Vorschau"));
        previewPanel_.setOpaque(false);

        javax.swing.GroupLayout previewPanel_Layout = new javax.swing.GroupLayout(previewPanel_);
        previewPanel_.setLayout(previewPanel_Layout);
        previewPanel_Layout.setHorizontalGroup(
            previewPanel_Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 194, Short.MAX_VALUE)
        );
        previewPanel_Layout.setVerticalGroup(
            previewPanel_Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 25, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(previewPanel_, gridBagConstraints);

        jButton1.setText("OK");
        jButton1.setOpaque(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jButton1, gridBagConstraints);

        jButton2.setText("Abbrechen");
        jButton2.setOpaque(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jButton2, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void boldCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_boldCheckBoxStateChanged
    update();
}//GEN-LAST:event_boldCheckBoxStateChanged

private void italicCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_italicCheckBoxStateChanged
    update();
}//GEN-LAST:event_italicCheckBoxStateChanged

private void backgroundPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_backgroundPanelMouseClicked
    Color c = JColorChooser.showDialog(this, "Farbe aussuchen", backgroundPanel.getBackground());
    if (c != null) {
        backgroundPanel.setBackground(c);
    }
}//GEN-LAST:event_backgroundPanelMouseClicked

private void colorPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_colorPanelMouseClicked
    Color c = JColorChooser.showDialog(this, "Farbe aussuchen", colorPanel.getBackground());
    if (c != null) {
        colorPanel.setBackground(c);
    }
    update();
}//GEN-LAST:event_colorPanelMouseClicked

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    dispose();
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    tabPanel.setStyle(buildSyntaxStyle());
    tab.update();
    dispose();
}//GEN-LAST:event_jButton1ActionPerformed

    private void update() {
        previewPanel.setStyle(buildSyntaxStyle());
    }

    private SyntaxStyle buildSyntaxStyle() {
        int style = 0;
        if (boldCheckBox.isSelected())
            style = style | Font.BOLD;
        if (italicCheckBox.isSelected())
            style = style | Font.ITALIC;

        Font font = new Font(
                    CollIDETextArea.getPropertyManager().getProperty("view.font"),
                    style,
                    CollIDETextArea.getPropertyManager().getIntProperty("view.fontsize")
                );

        Log.log("bg: "+backgroundPanel.getBackground());
        return new SyntaxStyle(colorPanel.getBackground(), backgroundPanel.getBackground(), font);
    }

    private PreviewPanel previewPanel;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel backgroundPanel;
    private javax.swing.JCheckBox boldCheckBox;
    private javax.swing.JPanel colorPanel;
    private javax.swing.JCheckBox italicCheckBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel panel1;
    private javax.swing.JPanel panel2;
    private javax.swing.JPanel previewPanel_;
    // End of variables declaration//GEN-END:variables

}
